/**
 * Copyright 2018 asiainfo Inc.
 **/
package com.myz.rabbitmq.common.listener;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;

import java.io.IOException;
import java.util.Map;

/**
 * @author maoyz on 2018/9/5
 * @version: v1.0
 */
// @Component
@RabbitListener(bindings = {@QueueBinding(
        value = @Queue(value = "", autoDelete = "", durable = "true"),
        exchange = @Exchange(name = "", durable = "true", type = ExchangeTypes.DIRECT, ignoreDeclarationExceptions = "true"),
        key = ""
)
})
public class EmailConsumer {
    private static final Logger logger = LoggerFactory.getLogger(EmailConsumer.class);

    /**
     * 消息消费 @RabbitHandler
     * 消息体 @Payload
     * 消息头 @Headers
     */
    @RabbitHandler
    public void endEmail(@Payload Object obj, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        logger.debug("EmailConsumer endEmail()");
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);

        // 手动ack
        channel.basicAck(deliveryTag, false);
    }
}
